import { Euler, Object3D, Vector3 } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param newRotation THREE.Euler
 * @param optionalOldRotation THREE.Euler
 * @constructor
 */
class SetRotationCommand extends Command {
	object:Object3D;
	oldRotation?:Euler;
	newRotation?:Euler;
	constructor( editor:Editor, object:Object3D, newRotation?:Euler, optionalOldRotation?:Euler ) {

		super( editor );

		this.type = 'SetRotationCommand';
		this.name = 'Set Rotation';
		this.updatable = true;

		this.object = object;

		if ( object !== undefined && newRotation !== undefined ) {

			this.oldRotation = object.rotation.clone();
			this.newRotation = newRotation.clone();

		}

		if ( optionalOldRotation !== undefined ) {

			this.oldRotation = optionalOldRotation.clone();

		}

	}

	execute() {
		if(this.newRotation)
		this.object.rotation.copy( this.newRotation );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	undo() {
		if(this.oldRotation)
		this.object.rotation.copy( this.oldRotation );
		this.object.updateMatrixWorld( true );
		this.editor.signals.objectChanged.dispatch( this.object );

	}

	update( command:SetRotationCommand ) {
		if(command.newRotation)
		this.newRotation?.copy( command.newRotation );

	}


}

export { SetRotationCommand };
